package com.example.lin.day8_31;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.lin.day8_31.pojo.Student;

import java.util.List;

public class StudentAdapter extends BaseAdapter {
    private List<Student> list;
    private Context context;

    public StudentAdapter(List<Student> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public List<Student> getList() {
        return list;
    }

    public void setList(List<Student> list) {
        this.list = list;
        notifyDataSetChanged();

    }

    public StudentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Student getItem(int position) {
        return list.get(position);
    }

    class ViewHolder {
        TextView tv_id;
        TextView tv_name;
        TextView tv_sex;
        TextView tv_age;

        public ViewHolder(View convertView) {
            tv_id = convertView.findViewById(R.id.tv_id);
            tv_name = convertView.findViewById(R.id.tv_name);
            tv_sex = convertView.findViewById(R.id.tv_sex);
            tv_age = convertView.findViewById(R.id.tv_age);
            convertView.setTag(this);
        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_list_item_1, parent, false);
            new ViewHolder(convertView);
        }
        ViewHolder vh = (ViewHolder) convertView.getTag();
        Student item = getItem(position);
        vh.tv_id.setText(String.valueOf(item.getId()));
        vh.tv_name.setText(item.getName());
        vh.tv_age.setText(String.valueOf(item.getAge()));
        vh.tv_sex.setText(item.getSex());
        return convertView;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }
}
